/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.AjaxBehaviorEvent;
import server.Database;

/**
 *
 * @author mszankin
 */
@ManagedBean(name = "LoginBean")
@SessionScoped
public class RegisterBean implements Serializable {

    String loginName;
    String password;
    private boolean extend = false;

    public void submit(AjaxBehaviorEvent event) {
        // validate loginname
        extend = true;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
    String status;
    String loginStatus;

    public String getLoginStatus() {
        return loginStatus;
    }

    public void setLoginStatus(String loginStatus) {
        this.loginStatus = loginStatus;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String Register(){
        Database db = new Database("snake", "karw", "pass", "localhost");

        db.connectMySQL();
        
        if(db.newUser(loginName, password)){
            status = "Nazwa uzytkownika wolna";
            loginStatus = "success2";
        }
        else{
            loginStatus = "failed";
        }
        return status;
    }
    
    public String checkLogin() {
        Database db = new Database("snake", "karw", "pass", "localhost");

        db.connectMySQL();

        if (!db.checkIfUsernameIsTaken(loginName)) {
            status = "Nazwa uzytkownika wolna";
            loginStatus = "success";
        } else {
            loginStatus = "failed";
        }

//        if(db.authUser(loginName, password)){
//            status = "Login is Success";
//            loginStatus = "success";
//            System.out.println("log success");
//        }
//        else {
//            loginStatus = "failed";
//            System.out.println("log fail");
//        }
        return status;
    }

    public String loginAction() {
        return loginStatus;
    }

    public void reset() {
        loginName = "";
        password = "";
        status = "";
        loginStatus = "";
    }
}
